﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TargetSpawner : MonoBehaviour {

	//tablica danych o celach gracza
	public static CharacterFactory.attrib[] targetsAttributes;
	//liczba celów w grze
	public static int targetNumber = 6;
	//odnośnik do obiektu modela postaci na GUI 
	public GameObject targetModel;
	//odnośnik do kontenera na postaci
	public GameObject characters;
	//odnośnik do kontenera waypointów
	public GameObject waypoints;
	
	//lista waypointów z których może wystartować cel
	private List<GameObject> waypointsList = new List<GameObject> ();

	public static int actualTarget;
	public static bool targetSpawned;
	// Use this for initialization
	void Start () {
		actualTarget = 0;
		targetSpawned = false;
		//wypełnienie listy waypointów
		foreach (Transform child in waypoints.transform)
		{
			if(child.GetComponent<Spawner>()!=null)
				waypointsList.Add(child.gameObject);
		}
		//stworzenie tablicy atrybutów celów. Cele mają 6 atrybutów: faceNmb, clothNmb, hairNmb, bandanaNmb, hoodNmb, sex
		targetsAttributes = new CharacterFactory.attrib[targetNumber];
	
		for (int i=0; i<targetNumber; i++) 
		{
			 
			if(Random.Range(0,2)==0)
				targetsAttributes[i] = CharacterFactory.rollForAttributes(true);
			else
				targetsAttributes[i] = CharacterFactory.rollForAttributes(false);
		}

	}
	
	// Update is called once per frame
	void Update () {
		if(!targetSpawned && Spawner.currentCharacterCounter > 50) {
			loadGUIModel(actualTarget);
			spawnTarget(actualTarget);
			targetSpawned = true;
			actualTarget++;
			if(actualTarget == targetNumber)
			{
				Screen.showCursor = true; 
				UIManagerScript.enabledWin = true;
				Application.LoadLevel ("Menu");
			}
		} 
	}

	public static bool compareAttributes(CharacterFactory.attrib newAttrib) {
		for (int i=0; i<targetNumber; i++) {
			if(newAttrib.sex == targetsAttributes[i].sex &&
			   newAttrib.faceNmb == targetsAttributes[i].faceNmb &&
			   newAttrib.clothNmb == targetsAttributes[i].clothNmb &&
			   newAttrib.hairNmb == targetsAttributes[i].hairNmb &&
			   newAttrib.bandanaNmb == targetsAttributes[i].bandanaNmb &&
			   newAttrib.hoodNmb == targetsAttributes[i].hoodNmb)
				return true;
		}
		return false;
	}

	void spawnTarget(int index) {
		//losowanie waypointa w którym wystartuje postać
		int waypointNumber = Random.Range(0,waypointsList.Count);
		if (targetsAttributes[index].sex) {
			//instancjonowanie nowej postaci, w pozycji wylosowanego waypointa
			GameObject man = Instantiate(
				Resources.Load<GameObject>("Characters/Prefabs/Man/man"),
				waypointsList[waypointNumber].transform.position,
				new Quaternion()) as GameObject;
			
			//przypisanie nowego prefaba do parenta (characters)
			man.transform.parent = characters.transform;

			//wybór twarzy postaci
			CharacterFactory.loadFace(true, man.transform, targetsAttributes[index].faceNmb);
			//wybór ubrania
			CharacterFactory.loadCloth(true, man.transform, targetsAttributes[index].clothNmb);
			//wybór włosów/czapki
			CharacterFactory.loadHead(true,
			                          man.transform,
			                          targetsAttributes[index].hairNmb,
			                          targetsAttributes[index].bandanaNmb,
			                          targetsAttributes[index].hoodNmb); 
			//dodanie komponentu sterującego postacią
			Waypoints newWaypoint = man.AddComponent<Waypoints>();
			newWaypoint.currentPoint = pickRandomWaypoint(waypointsList[waypointNumber].GetComponent<NextWaypoints>());
			newWaypoint.speed = Random.Range(0.15f,0.41f) + (float)Spawner.panicLevel * 0.1f;
			//tworzona teraz postać JEST CELEM
			man.GetComponent<Trigger>().asTarget = true;
		} else {

			//instancjonowanie nowej postaci, w pozycji wylosowanego waypointa
			GameObject woman = Instantiate(
				Resources.Load<GameObject>("Characters/Prefabs/Woman/woman"),
				waypointsList[waypointNumber].transform.position,
				new Quaternion()) as GameObject;
			//przypisanie nowego prefaba do parenta (characters)
			woman.transform.parent = characters.transform;

			//wybór twarzy postaci
			CharacterFactory.loadFace(false, woman.transform, targetsAttributes[index].faceNmb);
			//wybór ubrania
			CharacterFactory.loadCloth(false, woman.transform, targetsAttributes[index].clothNmb);
			//wybór włosów/czapki
			CharacterFactory.loadHead(false,
			                          woman.transform,
			                          targetsAttributes[index].hairNmb,
			                          targetsAttributes[index].bandanaNmb,
			                          targetsAttributes[index].hoodNmb); 
			//dodanie komponentu sterującego postacią
			Waypoints newWaypoint = woman.AddComponent<Waypoints>();
			newWaypoint.currentPoint = pickRandomWaypoint(waypointsList[waypointNumber].GetComponent<NextWaypoints>());
			newWaypoint.speed = Random.Range(0.15f,0.41f) + (float)Spawner.panicLevel * 0.1f;
			//tworzona teraz postać JEST CELEM
			woman.GetComponent<Trigger>().asTarget = true;
		}

	}


	void loadGUIModel(int index) {
		if(targetModel.transform.childCount != 0)
			Destroy(targetModel.transform.GetChild(0).gameObject);
		GameObject target;
		if(targetsAttributes[index].sex) {
			//instancjonowanie modelu postaci na GUI
			target = Instantiate(
				Resources.Load<GameObject>("GUIElements/targetMan"),
				new Vector3(),
				new Quaternion()) as GameObject;
			//wczytanie twarzy postaci jeśli ma inną niż podstawową
			CharacterFactory.loadFace(true, target.transform, targetsAttributes[index].faceNmb );
			//wczytanie ubrania
			CharacterFactory.loadCloth(true, target.transform, targetsAttributes[index].clothNmb);
			//wczytanie włosów/czapki jeśli ma coś na głowie
			CharacterFactory.loadHead(true, 
			         target.transform,
			         targetsAttributes[index].hairNmb,
			         targetsAttributes[index].bandanaNmb,
			         targetsAttributes[index].hoodNmb);  
		}
		else {
			//instancjonowanie modelu postaci na GUI
			target = Instantiate(
				Resources.Load<GameObject>("GUIElements/targetWoman"),
				new Vector3(),
				new Quaternion()) as GameObject;
			//wczytanie twarzy postaci jeśli ma inną niż podstawową
			CharacterFactory.loadFace(false, target.transform, targetsAttributes[index].faceNmb);
			//wczytanie ubrania
			CharacterFactory.loadCloth(false, target.transform, targetsAttributes[index].clothNmb);
			//wczytanie włosów/czapki jeśli ma coś na głowie
			CharacterFactory.loadHead(false, 
			         target.transform,
			         targetsAttributes[index].hairNmb,
			         targetsAttributes[index].bandanaNmb,
			         targetsAttributes[index].hoodNmb); 
		}
		target.transform.parent = targetModel.transform;
		//ustawienie parametrów
		target.transform.localScale = new Vector3(1f,1f,1f);
		target.transform.localPosition = new Vector3(0f,0f,0f);
	}

	GameObject pickRandomWaypoint(NextWaypoints nextWaypoints) 
	{
		int random = Random.Range(0,nextWaypoints.getNextWaypoints().Length);
		GameObject currentPoint = 
			nextWaypoints.getNextWaypoints() [random];
		return currentPoint;
	}
}
